<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\Players;
use App\Admin\Repositories\Course;
use App\Models\Team;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;


class CourseController extends AdminController
{
    public function index(Content $content)
    {
        return $content->title('课程')
            ->description('列表')
            ->body($this->grid());
    }

    public function create(Content $content)
    {
        return $content->title('课程')
            ->description('创建')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Course(), function (Grid $grid) {
            $grid->model()->orderBy('created_at' ,'desc');
            $grid->column('id')->sortable();
            $grid->column('class_name');
            $grid->column('team_id','上课球队')->display(function ($value){
                  return Team::getTeaMById($value);
            });
            $grid->column('class_start_time','上课时间')->display(function ($value){
                return date('Y-m-d H:i:s',$value);
            });
            $grid->column('class_end_time','上课结束')->display(function ($value){
                return date('Y-m-d H:i:s',$value);
            });
            $grid->column('class_status','课程状态')->display(function ($value){
                  if (time()>$this->class_start_time && time()<$this->class_end_time){
                      return '课程进行时';
                  }elseif (time()<$this->class_start_time){
                      return '课程还未开始';
                  }else{
                      return '课程已经结课';
                  }
            });
            $grid->filter(function (Grid\Filter $filter) {
                // 更改为 panel 布局
                $filter->panel();
                $filter->where('team_id', function ($query) {
                    $query->whereHas('team', function ($query) {
                        $query->where('team_name', 'like', "%{$this->input}%");
                    });
                }, '球队名称')->width(3);
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
//                $actions->disableDelete();
//                $actions->disableEdit();
//                $actions->disableQuickEidt();
                $actions->disableView();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Course(), function (Show $show) {
            $show->field('id');
            $show->field('class_name');
            $show->field('team_id');
            $show->field('class_time');
            $show->field('class_status')->display(function ($value){
                return $value ? '已下课' : '等待上课中';
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Course(), function (Form $form) {
            $form->display('id');
            $form->text('class_name');
            $form->select('team_id','上课球队')
                ->options('team/teamData');
            $form->datetime('class_start_time','上课时间')->saving(function ($value){
                return strtotime($value);
            });
            $form->datetime('class_end_time','下课时间')->saving(function ($value){
                return strtotime($value);
            });
//            $form->saved(function (Form $form) {
//                //执行短信发送逻辑
//               $userIds = Team::smsUserCourse($form->team_id);
//               $courseName = $form->class_name;
    //               $class_start_time = $form->class_start_time;
//               if ($userIds){
//                   //执行发送逻辑
//
//               }
//            });
        });
    }
    public function getCourse(){
        $teamDate = \App\Models\Course::all(['id','class_name as text'])->toArray();
        return $teamDate;
    }
}
